﻿using System;
using System.Data;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace VSSTQH
{
    public partial class POMapper
    {

        #region Methods With Relation

        public static POModel ToModel(PO entity)
        {
            if (entity == null)
            {
                return null;
            }
            POModel model = new POModel();
            model.POKey = entity.POKey;
            model.POCode = entity.POCode;
            model.Active = entity.Active;
            model.ActualWeight = entity.ActualWeight;
            model.ActualWeight2 = entity.ActualWeight2;
            model.CreatedByID = entity.CreatedByID;
            model.CreationDate = entity.CreationDate;
            model.FirstTime = entity.FirstTime;
            model.FirstWeight = entity.FirstWeight;
            model.Inbound = entity.Inbound;
            model.InboundWeight = entity.InboundWeight;
            model.ModifiedByID = entity.ModifiedByID;
            model.ModifiedDate = entity.ModifiedDate;
            model.PODate = entity.PODate;
            model.PoID = entity.PoID;
            model.PoType = entity.POType;
            model.VehicleID = entity.VehicleID;
            model.ProductID = entity.ProductID;
            model.ProductName = entity.ProductName;
            model.SecondTime = entity.SecondTime;
            model.SecondWeight = entity.SecondWeight;
            model.SeqInbound = entity.SeqInbound;
            model.Unit = entity.Unit;
            model.Unit2 = entity.Unit2;
            model.VendorID = entity.VendorID;
            model.VendorName = entity.VendorName;
            model.WBS = entity.WBS;
            model.Location = entity.Location;
            return model;
        }
        public static POModel ToModel(string[] csv)
        {
            if (csv == null || csv.Length < 11)
            {
                return null;
            }

            //•	Số Inbound Delivery \ Outbound Delivery 
            //•	STT trên Inbound trong SAP
            //•	Số PO
            //•	Mã Vendor
            //•	Tên Vendor
            //•	Mã hàng
            //•	Tên hàng
            //•	Kho
            //•	Số lượng inbound
            //•	Đơn vị tính
            //•	Tên hạng mục (WBS) 

            POModel model = new POModel();            
            model.Inbound = Decimal.Parse(csv[0]);
            model.SeqInbound = Int32.Parse(csv[1]);
            model.PoID = csv[2];
            model.VendorID = csv[3];
            int i = 4;
            List<string> array = new List<string>();
            if (csv[i].StartsWith("\""))
            {
                while (i < csv.Length)
                {
                    if (csv[i].EndsWith("\""))
                    {
                        array.Add(csv[i]);
                        i++;
                        break;
                    }
                    else
                    {
                        array.Add(csv[i]);
                        i++;
                    }
                }
            }
            else
            {
                array.Add(csv[i++]);
            }
            model.VendorName = string.Join(",", array.ToArray()).Trim('"');
            model.ProductID = csv[i++];
            array = new List<string>();
            if (csv[i].StartsWith("\""))
            {
                while (i < csv.Length)
                {
                    if (csv[i].EndsWith("\""))
                    {
                        array.Add(csv[i]);
                        i++;
                        break;
                    }
                    else
                    {
                        array.Add(csv[i]);
                        i++;
                    }
                }
            }
            else
            {
                array.Add(csv[i++]);
            }
            model.ProductName = string.Join(",", array.ToArray()).Trim('"');
            model.Location = csv[i++];

            decimal inbound;
            if(Decimal.TryParse(csv[i++], out inbound))
                model.InboundWeight = inbound;
            model.Unit = csv[i++];
            model.WBS = csv[i];

            return model;
        }

        public static string ToCSV(POModel model)
        {
            if (model == null)
            {
                return null;
            }

            //•	Số Inbound Delivery \ Outbound Delivery 
            //•	STT trên Inbound trong SAP
            //•	Số PO
            //•	Mã Vendor
            //•	Tên Vendor
            //•	Mã hàng
            //•	Tên hàng
            //•	Kho
            //•	Số lượng inbound
            //•	Số thực cân (KG)
            //•	Đơn vị tính 1
            //•	Số thực cân (Bó, PC)
            //•	Đơn vị tính 2
            //•	Số xe
            //•	Ngày giờ cân

            string[] csv = new string[15];
            csv[0] = model.Inbound.ToString("#");
            csv[1] = model.SeqInbound.ToString("#");
            csv[2] = model.PoID;
            csv[3] = model.VendorID;
            csv[4] = String.IsNullOrEmpty(model.VendorName) ? String.Empty : "\"" + model.VendorName + "\""; 
            csv[5] = model.ProductID;
            csv[6] = String.IsNullOrEmpty(model.ProductName) ? String.Empty : "\"" + model.ProductName + "\"";
            csv[7] = model.Location.ToString();
            csv[8] = model.InboundWeight.ToString("#");
            csv[9] = model.ActualWeight.ToString("#");
            csv[10] = model.Unit;
            csv[11] = model.ActualWeight2.ToString("#");
            csv[12] = model.Unit2;
            csv[13] = model.VehicleID;
            csv[14] = model.SecondTime.HasValue ? model.SecondTime.Value.ToString("dd/MM/yyyy HH:mm:ss") : String.Empty;

            return string.Join(",", csv);
        }

        public static PO ToEntity(PO entity, POModel model)
        {
            if (model == null)
            {
                return null;
            }
            entity.POKey = model.POKey;
            entity.POCode = model.POCode;
            entity.Active = model.Active;
            entity.ActualWeight = model.ActualWeight;
            entity.ActualWeight2 = model.ActualWeight2;
            entity.CreatedByID = model.CreatedByID;
            entity.CreationDate = model.CreationDate;
            entity.FirstTime = model.FirstTime;
            entity.FirstWeight = model.FirstWeight;
            entity.Inbound = model.Inbound;
            entity.InboundWeight = model.InboundWeight;
            entity.PODate = model.PODate;
            entity.PoID = model.PoID;
            entity.POType = model.PoType;
            entity.VehicleID = model.VehicleID;
            entity.ProductID = model.ProductID;
            entity.ProductName = model.ProductName;
            entity.SecondTime = model.SecondTime;
            entity.SecondWeight = model.SecondWeight;
            entity.SeqInbound = model.SeqInbound;
            entity.Unit = model.Unit;
            entity.Unit2 = model.Unit2;
            entity.VendorID = model.VendorID;
            entity.VendorName = model.VendorName;
            entity.WBS = model.WBS;
            entity.Location = model.Location;
            entity.ModifiedByID = model.ModifiedByID;
            entity.ModifiedDate = model.ModifiedDate;
            
            return entity;
        }

        #endregion

    }
}
